package com.xcf.controller.business.common;

import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.xcf.controller.base.BaseController;
import com.xcf.service.business.common.branchcompany.BranchCompanyService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value="/branch")
public class BranchCompanyController extends BaseController {
	String menuUrl = "branch/company"; //菜单地址(权限用)
	@Resource
	private BranchCompanyService branchcompany;
	@RequestMapping(value="/company")
public ModelAndView branch(){
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("/business/common/branchcompany/branchcompany");
		return mv;
		
		
	}

	//列表的显示及查询
		@RequestMapping(value="/list")
		@ResponseBody
		public void list(HttpServletResponse response) throws Exception{
			PageData pd = new PageData();
	         pd = this.getPageData();
	         if(pd.getString("BOOKING_TYPE")!=null && pd.getString("BOOKING_TYPE")!=""){
	         String BOOKING_TYPE= URLDecoder.decode(pd.getString("BOOKING_TYPE"), "utf-8");
	         pd.put("BOOKING_TYPE", BOOKING_TYPE);
	         }
	         if(pd.getString("COMPANY_NAME")!=null && pd.getString("COMPANY_NAME")!=""){
		         String COMPANY_NAME= URLDecoder.decode(pd.getString("COMPANY_NAME"), "utf-8");
		         pd.put("COMPANY_NAME", COMPANY_NAME);
		         }
	         if(pd.getString("SPELL_CODE")!=null && pd.getString("SPELL_CODE")!=""){
		         String SPELL_CODE= URLDecoder.decode(pd.getString("SPELL_CODE"), "utf-8");
		         pd.put("SPELL_CODE", SPELL_CODE);
		         }
	         List<PageData>list=branchcompany.seach(pd);
	         JSONArray arry = new JSONArray();
	         for (PageData branch : list) {
	        	 JSONObject ob = new JSONObject();
	        	 ob.put("ID", branch.get("ID"));
	        	 ob.put("BOOKING_TYPE", branch.get("BOOKING_TYPE"));
	        	 ob.put("COMPANY_NAME", branch.get("COMPANY_NAME")) ;
	        	 ob.put("SPELL_CODE", branch.get("SPELL_CODE"));
	        	 arry.add(ob);
			}
	         response.setContentType("text/html; charset=utf-8");         
	         response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"50"+"\""+","+"\""+"rows"+"\""+":"+arry+"}");	
		}
		
		
		//保存及更新
		@RequestMapping(value="/save")
		@ResponseBody
		public String save() throws Exception{
			PageData pd = new PageData();
			pd= this.getPageData();
			
			if(pd.getString("ID").isEmpty()){
				branchcompany.save(pd);
				}else{
				branchcompany.update(pd);
				}
			return null;
			
		}
		//删除
		@RequestMapping(value="/dele")
		@ResponseBody
		public String dele()throws Exception{
			PageData pd = new PageData();
			pd= this.getPageData();
			
			String data = pd.getString("array");
			
			JSONArray items = JSONArray.fromObject(data);
			for(int i=0,size=items.size();i<size;i++){
				JSONObject item = items.getJSONObject(i);
				String id = item.getString("id").trim();
				PageData pdq = new PageData();
				pdq.put("ID", id);
				branchcompany.delete(pdq);
			}
			return null;

}
}
